<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>会员管理<small>会员档案</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>会员管理</el-breadcrumb-item>
                    <el-breadcrumb-item>会员档案</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-input placeholder="档案编号/会员名/电话" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                        <el-button @click="findPage()" class="dalfBut">查询</el-button>
                        <!--@click绑定单击事件-->
                        <el-button type="primary" class="butT" @click="handleCreate()">添加会员</el-button>
                    </div>
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="fileNumber" label="档案编号" align="center"></el-table-column>
                        <el-table-column prop="name" label="会员名" align="center"></el-table-column>
                        <el-table-column label="性别" align="center">
                            <template slot-scope="scope">
                                <span>{{ scope.row.sex == '0' ? '不限' : scope.row.sex == '1' ? '男' : '女'}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="phoneNumber" label="电话号码" align="center"></el-table-column>
                        <el-table-column prop="remark" label="会员说明" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button><!--scope.row 表示该行的数据-->
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增会员" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="档案编号" prop="fileNumber">
                                            <el-input v-model="formData.fileNumber"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="会员名" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="性别">
                                            <el-select v-model="formData.sex">
                                                <el-option label="男" value="1"></el-option>
                                                <el-option label="女" value="2"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="年龄">
                                            <el-input v-model="formData.age"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="身份证号码">
                                            <el-input v-model="formData.idCard"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="手机号码">
                                            <el-input v-model="formData.phoneNumber"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="用户密码">
                                            <el-input v-model="formData.password"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="用户邮箱">
                                            <el-input v-model="formData.email"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="会员说明">
                                            <el-input v-model="formData.remark" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="档案编号" prop="fileNumber">
                                            <el-input v-model="formData.fileNumber"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="会员名" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="性别">
                                            <el-select v-model="formData.sex">
                                                <el-option label="男" value="1"></el-option>
                                                <el-option label="女" value="2"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="年龄">
                                            <el-input v-model="formData.age"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="身份证号码">
                                            <el-input v-model="formData.idCard"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="手机号码">
                                            <el-input v-model="formData.phoneNumber"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="用户密码">
                                            <el-input v-model="formData.password"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="用户邮箱">
                                            <el-input v-model="formData.email"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="会员说明">
                                            <el-input v-model="formData.remark" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见
                rules: {//校验规则；表单校验 blur为离焦事件
                    code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
                    name: [{ required: true, message: '会员名称为必填项', trigger: 'blur' }]
                }
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                this.findPage();//页面加载完就调用该方法
            },
            methods: {
                //编辑
                handleEdit() {
                    //发送ajax请求将用户输入的数据发送到后台
                    axios.post("/member/edit.do",this.formData).then((res) => {
                        if(res.data.flag){
                            //修改成功弹出提示
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            })
                        }else {
                            //修改失败
                            this.$message.error(res.data.message);
                        }
                    }).finally(()=>{
                        //无论成功与否都要刷新页面
                        this.findPage();
                        //隐藏编辑页面
                        this.dialogFormVisible4Edit=false;
                    })
                },
                //添加
                handleAdd () {
                    //表单校验
                    this.$refs['dataAddForm'].validate((valid) => {
                        if(valid){
                            //校验通过 发送ajax请求将数据发送到后台
                            //console.log(this.formData)将输入的数据在前端控制台显示
                            //ajax请求
                            axios.post("/member/add.do",this.formData).then((res) =>{//回调函数从服务器响应回来的对结果
                                this.dialogFormVisible=false; //无论保存是否成功都会关闭

                                if (res.data.flag){ //res.data拿到的就是响应回来的json对象
                                    //数据保存成功 调用findPage方法
                                    this.findPage();
                                    //弹出成功提示信息this.$message
                                    this.$message({
                                        message: res.data.message,
                                        type:'success'
                                    });
                                }else{
                                    //数据保存失败 弹出提示
                                    this.$message.error(res.data.message);
                                }
                            });
                        } else {
                            //校验不通过
                            this.$message.error("数据校验失败，请检查你的输入信息是否正确");
                            return false;
                        }
                    }).finally(()=>{
                        this.findPage();
                    });
                },
                //分页查询
                findPage() {//发送ajax请求 提交分页相关请求参数
                    //定义个一个json对象
                    var param = {
                        currentPage: this.pagination.currentPage,//当前页码
                        pageSize: this.pagination.pageSize,//每页显示条目
                        queryString: this.pagination.queryString//查询条件
                    };
                    axios.post("/member/findPage.do",param).then((res) => {
                        //解析响应的json数据 为模型赋值
                        this.pagination.total= res.data.total;//设置当前总条数
                        this.dataList = res.data.rows;//设置当前页要展示的分页列表数据
                    })
                },
                // 重置表单
                resetForm() {
                    this.formData={};//重置表达数据
                },
                // 弹出添加窗口
                handleCreate() {
                    this.dialogFormVisible=true;//修改为true可显示表单 false为隐藏表单
                    this.resetForm();
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    //将编辑窗口显示
                    this.dialogFormVisible4Edit=true;
                    //发送ajax请求 将想要编辑的数据id传入到后台查询数据库 将查询到数据回写到前端
                    axios.post("/member/findById.do?id="+row.id).then((res) =>{
                        if(res.data.flag){
                            //查询成功 给formData赋值
                            this.formData=res.data.data;
                        }else{
                            //查询失败
                            this.$message.error(res.data.message);
                        }
                    })
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //设置新页码
                    this.pagination.currentPage=currentPage;
                    //重新调用findPage方法
                    this.findPage();
                },
                // 删除
                handleDelete(row) {
                    //先给出提示 防止误删
                    this.$confirm("您确定要删除吗？","提示",{
                        type:'warning'
                    }).then(()=>{
                        //点击确定按钮发送ajax请求 将想要删除的数据id传入到后台 根据id删除
                        axios.post("/member/delete.do?id="+row.id).then((res) =>{
                            if(res.data.flag){
                                //删除成功 弹出提示
                                this.$message({
                                    type:'success',
                                    message: res.data.message
                                });
                            }else {
                                //删除失败
                                this.$message.error(res.data.message);
                            }
                        })
                    }).catch(()=>{
                        //用户取消删除操作
                        this.$message({
                            type:'info',
                            message:"用户取消操作"
                        })
                    }).finally(()=>{
                        this.findPage();
                    })
                }
            }
        })
    </script>
</html>